home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / qtools0.2-src.lha / src / libqtools / mip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  6.3 KB  |  205 lines

  1. #define    LIBQTOOLS_CORE
  2. #include "../include/libqtools.h"
  3.  
  4. /*
  5.  * MIP-tools
  6.  */
  7.  
  8. struct palpic *GetMipMap(HANDLE file, enum mipmapoffset MipLevel)
  9. {
  10.   struct mipmap MipMap;
  11.   struct palpic *Picture = 0;
  12.  
  13.   __read(file, &MipMap, sizeof(struct mipmap));
  14.  
  15.   if ((Picture = pmalloc(LittleLong(MipMap.width) >> MipLevel, LittleLong(MipMap.height) >> MipLevel, 0, MipMap.name))) {
  16.     __lseek(file, LittleLong(MipMap.offsets[MipLevel]) - sizeof(struct mipmap), SEEK_CUR);
  17.     __read(file, Picture->rawdata, (LittleLong(MipMap.width) >> MipLevel) * (LittleLong(MipMap.height) >> MipLevel));
  18.   }
  19.  
  20.   return Picture;
  21. }
  22.  
  23. struct palpic *ParseMipMap(struct mipmap *MipMap, enum mipmapoffset MipLevel)
  24. {
  25.   struct palpic *Picture = 0;
  26.  
  27.   if ((Picture = pmalloc(LittleLong(MipMap->width) >> MipLevel, LittleLong(MipMap->height) >> MipLevel, 0, MipMap->name))) {
  28.     __memcpy(Picture->rawdata, ((char *)MipMap) + LittleLong(MipMap->offsets[MipLevel]),
  29.          (LittleLong(MipMap->width) >> MipLevel) * (LittleLong(MipMap->height) >> MipLevel));
  30.   }
  31.  
  32.   return Picture;
  33. }
  34.  
  35. /*
  36.  * returns the offset or -1 for fail 
  37.  */
  38. bool PutMipMap(HANDLE file, struct palpic * Picture)
  39. {
  40.   struct mipmap MipMap;
  41.   int MipMapSize;
  42.   unsigned char *MipBody;
  43.   bool retval = FALSE;
  44.  
  45.   /*
  46.    * fix!!! OP_UPDATE offsets ??? 
  47.    */
  48.   MipMapSize = Picture->width * Picture->height;
  49.   if ((MipBody = (unsigned char *)tmalloc(MipMapSize))) {
  50.     short int x, y, num, dwidth = 1, dheight = 1, dshift = 1;
  51.     unsigned char *bodySrc, *bodyDst;
  52.     struct rgb *Palette = Picture->palette;
  53.  
  54.     __strncpy(MipMap.name, Picture->name, NAMELEN_MIP);
  55.     MipMap.height = LittleLong(Picture->height);
  56.     MipMap.width = LittleLong(Picture->width);
  57.  
  58.     MipMap.offsets[MIPMAP_0] = LittleLong(sizeof(struct mipmap));
  59.     MipMap.offsets[MIPMAP_1] = LittleLong(LittleLong(MipMap.offsets[MIPMAP_0]) + (MipMapSize / (1 * 1)));
  60.     MipMap.offsets[MIPMAP_2] = LittleLong(LittleLong(MipMap.offsets[MIPMAP_1]) + (MipMapSize / (2 * 2)));
  61.     MipMap.offsets[MIPMAP_3] = LittleLong(LittleLong(MipMap.offsets[MIPMAP_2]) + (MipMapSize / (4 * 4)));
  62.  
  63.     __write(file, &MipMap, sizeof(struct mipmap));
  64.     __write(file, bodySrc = Picture->rawdata, MipMapSize);
  65.  
  66.     for (num = 0; num < 3; num++) {
  67.       bodyDst = MipBody;
  68.       dwidth <<= 1;
  69.       dheight <<= 1;
  70.       dshift++;
  71.       for (y = 0; y < Picture->height; y += dheight) {
  72.     for (x = 0; x < Picture->width; x += dwidth) {
  73.       short int dx, dy;
  74.       short int R = 0, G = 0, B = 0;
  75.       struct rgb rawpix;
  76.  
  77.       for (dy = 0; dy < dheight; dy++) {
  78.         for (dx = 0; dx < dheight; dx++) {
  79.           short int palpix = (short int)bodySrc[((y + dy) * Picture->width) + x + dx];
  80.  
  81.           R += (short int)Palette[palpix].r;
  82.           G += (short int)Palette[palpix].g;
  83.           B += (short int)Palette[palpix].b;
  84.         }
  85.       }
  86.       rawpix.r = (unsigned char)(R >> dshift);
  87.       rawpix.g = (unsigned char)(G >> dshift);
  88.       rawpix.b = (unsigned char)(B >> dshift);
  89.       *bodyDst++ = Match(&rawpix, Palette);
  90.     }
  91.       }
  92.       __write(file, MipBody, MipMapSize / (dheight * dwidth));
  93.       mprogress(3, num + 1);
  94.     }
  95.     tfree(MipBody);
  96.     retval = TRUE;
  97.   }
  98.   else
  99.     eprintf(failed_memory, MipMapSize, "mipmap");
  100.  
  101.   return retval;
  102. }
  103.  
  104. bool PutMipMap0(HANDLE file, struct palpic * Picture)
  105. {
  106.   struct mipmap MipMap;
  107.  
  108.   __strncpy(MipMap.name, Picture->name, NAMELEN_MIP);
  109.   MipMap.height = LittleLong(Picture->height);
  110.   MipMap.width = LittleLong(Picture->width);
  111.  
  112.   MipMap.offsets[MIPMAP_0] = LittleLong(sizeof(struct mipmap));
  113.   MipMap.offsets[MIPMAP_1] = 0;
  114.   MipMap.offsets[MIPMAP_2] = 0;
  115.   MipMap.offsets[MIPMAP_3] = 0;
  116.  
  117.   __write(file, &MipMap, sizeof(struct mipmap));
  118.   __write(file, Picture->rawdata, Picture->width * Picture->height);
  119.  
  120.   return TRUE;
  121. }
  122.  
  123. bool PasteMipMap(struct mipmap * MipMap, struct palpic * Picture)
  124. {
  125.   int MipMapSize;
  126.   unsigned char *MipBody;
  127.   bool retval = FALSE;
  128.  
  129.   /*
  130.    * fix!!! OP_UPDATE offsets ??? 
  131.    */
  132.   MipMapSize = Picture->width * Picture->height;
  133.   if ((MipBody = (unsigned char *)tmalloc(MipMapSize))) {
  134.     short int x, y, num, dwidth = 1, dheight = 1, dshift = 1, pos;
  135.     unsigned char *bodySrc, *bodyDst;
  136.     struct rgb *Palette = Picture->palette;
  137.  
  138.     __strncpy(MipMap->name, Picture->name, NAMELEN_MIP);
  139.     MipMap->height = LittleLong(Picture->height);
  140.     MipMap->width = LittleLong(Picture->width);
  141.  
  142.     MipMap->offsets[MIPMAP_0] = LittleLong(sizeof(struct mipmap));
  143.     MipMap->offsets[MIPMAP_1] = LittleLong(LittleLong(MipMap->offsets[MIPMAP_0]) + (MipMapSize / (1 * 1)));
  144.     MipMap->offsets[MIPMAP_2] = LittleLong(LittleLong(MipMap->offsets[MIPMAP_1]) + (MipMapSize / (2 * 2)));
  145.     MipMap->offsets[MIPMAP_3] = LittleLong(LittleLong(MipMap->offsets[MIPMAP_2]) + (MipMapSize / (4 * 4)));
  146.  
  147.     pos = sizeof(struct mipmap);
  148.     __memcpy(((char *)MipMap) + pos, bodySrc = Picture->rawdata, MipMapSize);
  149.     pos += MipMapSize;
  150.  
  151.     for (num = 0; num < 3; num++) {
  152.       bodyDst = MipBody;
  153.       dwidth <<= 1;
  154.       dheight <<= 1;
  155.       dshift++;
  156.       for (y = 0; y < Picture->height; y += dheight) {
  157.     for (x = 0; x < Picture->width; x += dwidth) {
  158.       short int dx, dy;
  159.       short int R = 0, G = 0, B = 0;
  160.       struct rgb rawpix;
  161.  
  162.       for (dy = 0; dy < dheight; dy++) {
  163.         for (dx = 0; dx < dheight; dx++) {
  164.           short int palpix = (short int)bodySrc[((y + dy) * Picture->width) + x + dx];
  165.  
  166.           R += (short int)Palette[palpix].r;
  167.           G += (short int)Palette[palpix].g;
  168.           B += (short int)Palette[palpix].b;
  169.         }
  170.       }
  171.       rawpix.r = (unsigned char)(R >> dshift);
  172.       rawpix.g = (unsigned char)(G >> dshift);
  173.       rawpix.b = (unsigned char)(B >> dshift);
  174.       *bodyDst++ = Match(&rawpix, Palette);
  175.     }
  176.       }
  177.       __memcpy(((char *)MipMap) + pos, MipBody, MipMapSize / (dheight * dwidth));
  178.       pos += MipMapSize / (dheight * dwidth);
  179.       mprogress(3, num + 1);
  180.     }
  181.     tfree(MipBody);
  182.     retval = TRUE;
  183.   }
  184.   else
  185.     eprintf(failed_memory, MipMapSize, "mipmap");
  186.  
  187.   return retval;
  188. }
  189.  
  190. bool PasteMipMap0(struct mipmap * MipMap, struct palpic * Picture)
  191. {
  192.   __strncpy(MipMap->name, Picture->name, NAMELEN_MIP);
  193.   MipMap->height = LittleLong(Picture->height);
  194.   MipMap->width = LittleLong(Picture->width);
  195.  
  196.   MipMap->offsets[MIPMAP_0] = LittleLong(sizeof(struct mipmap));
  197.   MipMap->offsets[MIPMAP_1] = 0;
  198.   MipMap->offsets[MIPMAP_2] = 0;
  199.   MipMap->offsets[MIPMAP_3] = 0;
  200.  
  201.   __memcpy(((char *)MipMap) + sizeof(struct mipmap), Picture->rawdata, Picture->width * Picture->height);
  202.  
  203.   return TRUE;
  204. }
  205.